package com.heima.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.PageResult;
import com.heima.mapper.SkuMapper;
import com.heima.pojo.Order;
import com.heima.mapper.OrderMapper;
import com.heima.pojo.vo.OrderCollectVo;
import com.heima.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 服务实现类
 *
 * @author xjy
 * @since 2024-06-06
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private SkuMapper skuMapper;

    /**
     * 合作商分成汇总
     *
     * @param pageResult
     * @param start
     * @param end
     * @param partnerName
     * @return
     */
    @Override
    public PageResult<OrderCollectVo> partnerCollect(PageResult<OrderCollectVo> pageResult, String start, String end, String partnerName) {
        Page<OrderCollectVo> page = new Page<>(pageResult.getPageIndex(), pageResult.getPageSize());
        orderMapper.pagePartnerCollect(page, start, end, partnerName);
        return pageResult.of(page, OrderCollectVo.class);
    }

    /**
     * 根据地区汇总销售额数据(销售量分布)
     *
     * @param start 开始时间
     * @param end   结束时间
     * @return
     */
    @Override
    public Map<String, Object> regionCollect(String start, String end) {
        LocalDateTime startTime = LocalDateTimeUtil.parse(start, "yyyy-MM-dd");
        LocalDateTime endTime = LocalDateTimeUtil.parse(end, "yyyy-MM-dd");
        List<Map<String, Object>> list = orderMapper.regionCollect(startTime, endTime);
        String[] xAxis = new String[list.size()];
        Integer[] series = new Integer[list.size()];
        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> map = list.get(i);
            xAxis[i] = map.get("xAxis").toString();
            series[i] = Integer.parseInt(map.get("series").toString());
        }
        Map<String, Object> map = Map.of("xAxis", xAxis, "series", series);
        return map;
    }

    @Override
    public List<Map<String, Object>> skuTop(Integer topValue, LocalDateTime startTime, LocalDateTime endTime) {
        return skuMapper.topSku(topValue, startTime, endTime);
    }

    @Override
    public Map<String, Object> amountCollect(Integer collectType, String start, String end) {
        LocalDateTime startTime = LocalDateTimeUtil.parse(start, "yyyy-MM-dd");
        LocalDateTime endTime = LocalDateTimeUtil.parse(end, "yyyy-MM-dd");
        List<Map<String, Object>> mapList = orderMapper.amountCollect(collectType, startTime, endTime);
        String[] xAxis = new String[mapList.size()];
        Integer[] series = new Integer[mapList.size()];
        for (int i = 0; i < mapList.size(); i++) {
            xAxis[i] = mapList.get(i).get("xAxis").toString();
            series[i] = Integer.parseInt(mapList.get(i).get("series").toString());
        }
        Map<String,Object> map=new HashMap<>();
        map.put("xAxis",xAxis);
        map.put("series",series);
        return map;
    }

}
